package com.myjava.managesystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myjava.managesystem.common.CustomException;
import com.myjava.managesystem.common.R;
import com.myjava.managesystem.dto.SetmealDto;
import com.myjava.managesystem.entity.Setmeal;
import com.myjava.managesystem.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    SetmealService setmealService;


    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> saveSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.saveSetmealWithDish(setmealDto);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<IPage<SetmealDto>> getPage(@RequestParam int page, @RequestParam int pageSize){
        return R.success(setmealService.getPageWithCategory(page,pageSize));
    }

    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> deleteSetmeal(@RequestParam List<Long> ids){
        setmealService.removeSetmealWithDish(ids);
        return R.success("删除成功");
    }

    @PostMapping("/status/{st}")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> updateStatus(@PathVariable int st,@RequestParam List<Long> ids){
        setmealService.updateStatusWithDish(st,ids);
        return R.success("修改成功");
    }

    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#categoryId+'_'+#status")
    public R<List<Setmeal>> getList(@RequestParam Long categoryId,int status){
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId,categoryId);
        wrapper.eq(Setmeal::getStatus,status);
        return R.success(setmealService.list(wrapper));
    }
}
